<?php

namespace App\Http\Controllers;
use DB;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Pagination\LengthAwarePaginator;
header("content-type:text/html;charset=utf-8");
class BuyController extends BaseController
{
    public function index(){
    	 $brand = DB::table('brand')->limit(18)->get();
    	 $brands = DB::table('brand')->get();
    	 $series = DB::table('car_series')->get();
    	 $price = DB::table('price')->get();
    	 //print_R($price);die;
    	 $car_age = DB::table('car_age')->get();
    	 //print_R($car_age);die;
    	 $car = DB::table('car') ->get();
    	 //print_R($car);die;
    	 return view('buy', ['brand' => $brand,'brands'=>$brands,'series'=>$series,'price'=>$price,'age'=>$car_age,'car'=>$car]);
    }
    /*
	*车系替换
	*/
    public function series(){
    	$b_id=$_GET['b_id'];
    	//echo $b_id;die;
    	$series = DB::table('car_series')->where("b_id",$b_id)->get();
    	//print_R($series);
    	return view('a', ['series'=>$series]);
    	
    }
    /*
	*汽车详情
    */
    public function detial(){
    	$c_id=$_GET['c_id'];
    	//echo $c_id;die;
    	$car=DB::table('car')->where('c_id',$c_id)->get();
    	
    	 $cardetial = DB::table('car')
            ->join('picture', 'car.c_id', '=', 'picture.c_id')
            ->select('car.*', 'picture.*')
            ->where('car.c_id',$c_id)
            ->get();
          //print_R($cardetial);die;
        return view('car_detial', ['car'=>$car,'cardetial'=>$cardetial]);    
    }
    /*
	*搜索
    */
    public function select(){
    	$brand = DB::table('brand')->limit(18)->get();
    	 $brands = DB::table('brand')->get();
    	 $series = DB::table('car_series')->get();
    	 $price = DB::table('price')->get();
    	 $car_age = DB::table('car_age')->get();
    	// $car = DB::table('car') ->get();
    	 //print_R($car);die;
    	$b_id=isset($_GET['b_id'])?$_GET['b_id']:"";
    	$s_id=isset($_GET['s_id'])?$_GET['s_id']:"";
    	//echo $s_id;die;
    	$prices=isset($_GET['price'])?$_GET['price']:"";
    	if(!empty($prices)){
    		echo $prices;
    		$aa=explode(",",$prices);
	    	//print_R($aa);die;
	    	$minprice=$aa[0];
	    	$maxprice=$aa[1];
    	}
    	$age=isset($_GET['age'])?$_GET['age']:"";
    	if(!empty($age)){
    		$bb=explode(",",$age);
	    	//print_R($bb);die;
	    	$minage=$bb[0];
	    	$maxage=$bb[1];
    	}
    	 if($b_id!="") 
          {
               $where=" b_id = $b_id"; //指明是哪个表中的c_id  因为才c_id 是引起歧义的 两个表中都有c_id 
          }else
          {
               $where ="  1";  //只要第一个条件不空  后面就可加and 条件
          }
           if($s_id!="")
          {
          	//echo $s_id;die;
               $where.="  and s_id = $s_id";  //同上
          }
			//echo $where;die;
           /*if($minprice!="" && $maxprice!="")
          {
               $where.="  and p_id between $minprice and $maxprice"; //同上
          }*/


            	

          $sql="select * from car where".$where;
          //echo $sql;die;
          $row=DB::select($sql);
          //print_R($row);die;
    	 return view('select', ['car'=>$row]);

    }
}
